# !/usr/bin/env python
# -*- coding:utf-8 -*-
# Author : hai.long
import uuid
import requests
import platform
import threading
from _server.consumer import start_consumer, start_producer
from utils.common import get_local_ip


def register_agent():
    address = "http://192.168.0.99:8000"
    # host = "http://autotest.zaxh.cn"
    url = f"{address}/api/common/register/playwright/agent"
    ip = get_local_ip()
    system_name = platform.system()
    payload = {"name": str(uuid.uuid4()), "ip": ip, "port": 5000, "platform": system_name}
    response = requests.post(url, json=payload)
    if response.status_code == 200:
        print(response.json())
        if response.json()["status"] == 1000:
            return True
        else:
            return False
    else:
        return False


if register_agent():
    print("playwright agent注册成功")
    t1 = threading.Thread(target=start_producer, name="producer")
    t1.start()
    start_consumer()
else:
    raise ValueError("playwright agent注册失败，请检查master地址是否存在")
